package com.example.pets.view.fragment.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.Post;

import java.util.List;

public class PostDontaiAdapter extends RecyclerView.Adapter<PostDontaiAdapter.PostViewHolder> {

    private Context context;
    private List<Post> postList;

    public PostDontaiAdapter(Context context, List<Post> postList) {
        this.context = context;
        this.postList = postList;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_post, parent, false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post = postList.get(position);

        holder.tvAuthorId.setText(post.getTitle());
        holder.tvContent.setText(post.getContent());
        holder.tvLikesCount.setText("点赞数: " + post.getLikesCount());
        holder.tvCommentsCount.setText("评论数: " + post.getCommentsCount());

        Glide.with(context)
                .load(post.getAuthorurl()==null?"https://b0.bdstatic.com/0df6c8c7f109aa7b67e7cb15e6f8d025.jpg@h_1280":post.getAuthorurl()) // 你需要在 Post 模型中添加 getAuthorAvatarUrl()
                .circleCrop() // 圆形裁剪
                .into(holder.iv_author_avatar);
        // 处理图片
        if (post.getImageUrls() != null && !post.getImageUrls().isEmpty()) {
            holder.llImages.removeAllViews();
            for (String imageUrl : post.getImageUrls()) {
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setAdjustViewBounds(true);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                imageView.setLayoutParams(params);
                Glide.with(context)
                        .load(imageUrl)
                        .into(imageView);
                holder.llImages.addView(imageView);
                Log.e("===图片",imageUrl);
            }
        } else {
            holder.llImages.setVisibility(View.GONE);
        }


    }

    private String formatTimestamp(long timestamp) {
        // 实现时间戳转换为可读字符串的方法
        // 此处简单返回时间戳字符串作为示例
        return String.valueOf(timestamp);
    }

    @Override
    public int getItemCount() {
        return postList.size();
    }

    static class PostViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_author_avatar;
        TextView tvAuthorId, tvContent, tvLikesCount, tvCommentsCount, tvCreatedAt;
        LinearLayout llImages;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_author_avatar = itemView.findViewById(R.id.iv_author_avatar);
            tvAuthorId = itemView.findViewById(R.id.tv_author_id);
            tvContent = itemView.findViewById(R.id.tv_content);
            llImages = itemView.findViewById(R.id.ll_images);
            tvLikesCount = itemView.findViewById(R.id.tv_likes_count);
            tvCommentsCount = itemView.findViewById(R.id.tv_comments_count);
            tvCreatedAt = itemView.findViewById(R.id.tv_created_at);
        }
    }
}